package com.imu.purse.modules.app.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.app.dao.ImuUserInvMapper;
import com.imu.purse.modules.app.entity.ImuUserInvEntity;
import com.imu.purse.modules.app.service.ImuUserInvService;

/**
 * 用户推荐邀请
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-21 11:11:59
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class ImuUserInvServiceImpl extends BaseService<ImuUserInvEntity> implements ImuUserInvService 
{
	@Override
    @Transactional
    public int save(final ImuUserInvEntity imuUserInv)
	{
		imuUserInv.setTimeCreate(new Date());
    	imuUserInv.setTimeUpdate(new Date());
    	return super.insert(imuUserInv);
	}
	
	@Override
	@Transactional
    public int update(final ImuUserInvEntity imuUserInv) 
    {
    	imuUserInv.setTimeUpdate(new Date());
    	return mapper.updateByPrimaryKeySelective(imuUserInv);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final ImuUserInvEntity imuUserInv)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(imuUserInv)))
    	{
        	len = this.insert(imuUserInv);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    public ImuUserInvEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<ImuUserInvEntity> queryList(final ImuUserInvEntity imuUserInv)
    {
    	if (imuUserInv.getPage() != null && imuUserInv.getLimit() != null)
    	{
            PageHelper.startPage(imuUserInv.getPage(), imuUserInv.getLimit());
        }
    	
    	return mapper.findByPage(imuUserInv);
    }
    
	@Autowired
	private ImuUserInvMapper mapper;
}
